# coding:utf-8
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication, QFrame, QHBoxLayout, QGridLayout, QSizePolicy
from qfluentwidgets import (NavigationItemPosition, SubtitleLabel, MSFluentWindow, setFont)
from ui.football import Football
import sys

from main_crawler import run_crawler


class Widget(QFrame):
    def __init__(self, text: str, parent=None):
        super().__init__(parent=parent)
        self.label = SubtitleLabel(text, self)
        self.hBoxLayout = QHBoxLayout(self)

        setFont(self.label, 24)
        self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.hBoxLayout.addWidget(self.label, 1, Qt.AlignmentFlag.AlignCenter)
        self.setObjectName(text.replace(' ', '-'))

class FootballWidget(QFrame):
    def __init__(self, text: str, queue, stop_event, parent=None):
        super().__init__(parent=parent)
        self.__football = Football(queue, stop_event)
        self.gridLayout = QGridLayout(self)
        self.gridLayout.addWidget(self.__football)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)  # 设置内部布局的边距
        self.gridLayout.setSpacing(0)  # 设置布局间距为 0
        # 设置控件的尺寸策略，使其可以扩展
        self.__football.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)

        self.setObjectName(text)

class MainWindow(MSFluentWindow):
    
    def __init__(self, queue, stop_event):
        super().__init__()
        
        # create sub interface
        self.__footballInterface = FootballWidget("football", queue, stop_event)
        self.__basketballInterface = Widget('还没开始写')
        self.__settingInterface = Widget('设置 Interface')
        self.__robotInterface = Widget('好像没啥卵用')

        self.initNavigation()
        self.initWindow()
    

    def initNavigation(self):
        self.addSubInterface(self.__footballInterface, QIcon("ui/images/football.png"), '足球', QIcon("ui/images/football-fill.png")) # 足球页面
        self.addSubInterface(self.__basketballInterface, QIcon("ui/images/basketball.png"), '篮球', QIcon("ui/images/basketball-fill.png"))  # 篮球页面
        self.addSubInterface(self.__settingInterface, QIcon("ui/images/setting.png"), '设置', QIcon("ui/images/setting-fill.png"), NavigationItemPosition.BOTTOM) # 设置页面
        self.addSubInterface(self.__robotInterface, QIcon("ui/images/robot.png"), '我的', QIcon("ui/images/robot-fill.png"), NavigationItemPosition.BOTTOM) # 我的页面
        
        # 确保默认显示第一个页面
        self.navigationInterface.setCurrentItem(self.__footballInterface.objectName())


    def initWindow(self):
        self.resize(444, 888)   # 设置宽高
        self.setMinimumWidth(444)  # 设置最小宽度
        self.setWindowIcon(QIcon('ui/images/代理商.我的对冲分.png'))   # 设置图标
        self.setWindowTitle('No rework')  # 设置标题

        # 设置窗口打开位置
        desktop = QApplication.screens()[0].availableGeometry() 
        w, h = desktop.width(), desktop.height()
        self.move(w//8 - self.width()//2, h//2 - self.height()//2)

def run_ui():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.setWindowOpacity(0.95)
    window.show()
    app.exec()
